#Binary parser template

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class SevenSouls_msh(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(SevenSouls_msh,self).__init__("MSH")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_vertices(self, meshName, numVerts):
        
        for i in xrange(numVerts):
            vx, vy, vz, nx, ny, nz, tu, tv = self.inFile.read_float(8)
            
    def parse_faces(self, meshName, numFaces):
        
        faceDir = 1
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_long()
    
    def parse_weights1(self, meshName, count):
        
        for i in xrange(count):
            boneID, vertID = self.inFile.read_long()
            weight = self.inFile.read_float()
            v2 = self.inFile.read_long()
            w2 = self.inFile.read_float()
            v3 = self.inFile.read_long()
            w3 = self.inFile.read_float()
            v4 = self.inFile.read_long()
            w4 = self.inFile.read_float()
            
    def parse_weights2(self, meshName, count):
        
        for i in xrange(count):
            bone1, vertID = self.inFile.read_long(2)
            weight = self.inFile.read_float()
            v2 = self.inFile.read_long()
            w2 = self.inFile.read_float()
            v3 = self.inFile.read_long()
            w3 = self.inFile.read_float()
            v4 = self.inFile.read_long()
            w4 = self.inFile.read_float()
        
    def parse_type1(self):
        
        boneFile = self.inFile.read_long()
        
        meshName = self.inFile.read_string(self.inFile.read_long())
        numVerts, numFace, numWeight1, numWeight2 = self.inFile.read_long(4)
        self.parse_vertices(meshName, numVerts)
        self.parse_faces(meshName, numFaces)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        idstring = self.inFile.read_string(4)
        if idstring != "MESH":
            self.parse_type1()
        else:
            self.parse_type2()
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = SevenSouls_msh(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "", ""

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)